import { POST, GET } from './axios.config.js'
// import { downloadInLink } from '@utility/utility.js'

const base = window.config.ip
const module = '/rcdg-zbkh'
const projectName = '/topnet-assess'
const projectName1 = '/rcdg-record'

/** *********************** 公共 *************************/

// 多文件上传
// export const uploadFiles = (fileForm, onProgress) =>
//   POST(`${base}${module}/sa-file-baseInfo/uploadFiles`, fileForm, {
//     onUploadProgress: event => {
//       const progress = (event && Math.floor((event.loaded * 100) / event.total)) || 0
//       if (onProgress) {
//         onProgress(progress)
//       }
//     },
//     headers: {
//       hideLoading: true,
//     },
//   })
export const uploadFiles = (fileForm, onProgress) =>
  POST(`${base}${projectName}/sa-file-baseInfo/uploadFiles`, fileForm, {
    onUploadProgress: event => {
      const progress = (event && Math.floor((event.loaded * 100) / event.total)) || 0
      if (onProgress) {
        onProgress(progress)
      }
    },
    headers: {
      hideLoading: true,
      'Content-Type': 'multipart/form-data',
    },
  })
// 工作通知、制度文件上传附件
export const uploadFileNotice = (fileForm, onProgress) =>
  POST(`${base}/jx-backstage/fileBaseInfo/uploadFiles`, fileForm, {
    onUploadProgress: event => {
      const progress = (event && Math.floor((event.loaded * 100) / event.total)) || 0
      if (onProgress) {
        onProgress(progress)
      }
    },
    headers: {
      hideLoading: true,
      'Content-Type': 'multipart/form-data',
    },
  })
// export const downloadFile = id => {
//   const url = `${base}${projectName1}/file/skipValidation/downLoad${id}`
//   downloadInLink(url)
// }
export const downloadFile = id => `${base}${projectName1}/file/skipValidation/download${id}`

export const downloadFileByServer = id => {
  GET(`${base}${module}/file/downLoad`, { id }).then(res => {
    const { data, headers } = res
    const fileName = headers['content-disposition'].replace(/\w+;filename=(.*)/, '$1')

    // const blob = new Blob([data], { type: headers['content-type'] })
    const dom = document.createElement('a')
    const url = window.URL.createObjectURL(data)
    dom.href = url
    dom.download = decodeURI(fileName)
    dom.style.display = 'none'
    document.body.appendChild(dom)
    dom.click()
    dom.parentNode.removeChild(dom)
    window.URL.revokeObjectURL(url)
  })
}

export const getFilePreviewUrl = url => {
  // TODO 临时解决：安徽效能网内网无法下载问题
  // return url.replace('www.xnah.gov.cn', '59.203.26.122:8201').replace('http://', 'https://')
  return url
}

// 机构列表
export const getOrgList = () => POST(`${base}/public-uias-api/uias-organ/listOrganTreeAll`)

// ----------------------------------------

// -----待办----------------
// 获取待办列表，param：0-代办，1-已办
export const listNeedDeal = ({ currentPage = 1, pageSize = 10, param = '0' }) =>
  POST(`${base}${module}/std-need-deal/listNeedDeal`, { currentPage, pageSize, param })
// 更新待办已读
export const updateNeedDealRead = id => POST(`${base}${module}/std-need-deal/updateNeedDealRead`, { id })
// -------待办------------------

// -------消息---------+
// 获取消息列表
export const listNotice = p => POST(`${base}${module}/std-notice/listNotice`, p)
// 更新消息未读
export const updateNoticeRead = p => POST(`${base}${module}/std-notice/updateNoticeRead`, p)
// -------消息---------
const publicPath = process.env.NODE_ENV === 'production' ? './' : '/'
// 获取地图数据
export const getMapData = url => GET(`${publicPath}${url}`)
// 获取字典信息
export const listCodePublic = code => POST(`${base}${module}/question/listCodePublic`, { oneParam: code })

// 记录用户登录次数
export const getUserLoginRecordCount = userId =>
  POST(`${base}/public-uias-api/uias-user-login-record/getUserLoginRecordCount`, {
    tenantId: 'KH',
    oneParam: userId,
  })
